<template>
  <div class="login-container">
    <div class="login-form-content">
      <div class="login-bg">
        <img src="../../assets/login-bg.png" alt="" />
      </div>
      <div class="s-d-flex s-ai-center form-rright">
        <div class="w100p">
          <div class="title-container">
            <h2 class="title">欢迎登录</h2>
          </div>
          <a-tabs v-model:activeKey="activeKey">
            <a-tab-pane key="1" tab="账号登录">
              <a-form
                ref="formRef"
                :model="formState"
                :rules="rules"
                :wrapper-col="wrapperCol"
              >
                <a-form-item ref="name" name="name">
                  <a-input
                    size="large"
                    v-model:value="formState.name"
                    placeholder="用户名"
                  >
                    <template #prefix>
                      <user-outlined style="color: #ccc" type="user" />
                    </template>
                  </a-input>
                </a-form-item>
                <a-form-item ref="password" name="password">
                  <a-input
                    size="large"
                    v-model:value="formState.password"
                    placeholder="密码(6-12位)"
                  >
                    <template #prefix>
                      <ShoppingOutlined style="color: #ccc" />
                    </template>
                  </a-input>
                </a-form-item>
              </a-form>
            </a-tab-pane>
            <a-tab-pane key="2" tab="验证码登录" force-render>
              <a-form
                ref="formRef2"
                :model="formState2"
                :rules="rules2"
                :wrapper-col="wrapperCol"
              >
                <a-form-item ref="phone" name="phone">
                  <a-input
                    size="large"
                    v-model:value="formState2.phone"
                    placeholder="手机号"
                  >
                    <template #prefix>
                      <PhoneOutlined style="color: #ccc" />
                    </template>
                  </a-input>
                </a-form-item>
                <a-form-item ref="smsCode" name="smsCode">
                  <a-input
                    size="large"
                    v-model:value="formState2.smsCode"
                    placeholder="验证码"
                  >
                    <template #prefix>
                      <ShoppingOutlined style="color: #ccc" />
                    </template>
                    <template #suffix>
                       
                      <a-button type="link" @click="getCode"
                        >{{showTime?`${timer}s重新获取验证码`:'获取验证码'}}</a-button
                      >
                    </template>
                  </a-input>
                </a-form-item>
              </a-form>
            </a-tab-pane>
          </a-tabs>
           <div
                      class="s-ta color-grayr s-pointer"
                      style="text-align:right;margin-bottom:10px"
                      @click="handleForgetFace"
                    >
                      忘记密码?
                    </div>
          <a-button size=large type="primary" block @click.native.prevent="handleLoginPhone">登录</a-button>
         <div
                  class="color-gray s-tac s-pointer"
                  style="margin-top:20px"
                  @click="handleRegister"
                >
                  申请注册
                </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import md5 from "md5";
import { defineComponent, reactive, ref ,toRef} from "vue";
import { message } from 'ant-design-vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import {
  UserOutlined,
  ShoppingOutlined,
  PhoneOutlined,
} from "@ant-design/icons-vue";
import * as userApi from "@/api/user";
export default defineComponent({
  name: "Login",
  components: {
    UserOutlined,
    ShoppingOutlined,
    PhoneOutlined,
  },
  setup() {
    const router = useRouter()
     const store = useStore()
    const activeKey = ref('1')
    const formRef = ref();
    const formState = reactive({
      name: "",
      password: "",
    });
    const rules = {
      name: [
        {
          required: true,
          message: "请输入用户名",
          trigger: "blur",
        },
      ],
      password: [
        {
          required: true,
          message: "请输入密码",
          trigger: "blur",
        },
        {
          min: 6,
          max: 12,
          message: "请输入6-12位密码",
          trigger: "blur",
        },
      ],
    };
      //申请注册
    const handleRegister = ()=>{
     
      router.push({ path: '/register' });
    }
      //忘记密码
    const handleForgetFace = ()=>{
       router.push({ path: '/forgetface'})
    }
    // 登录
    const handleLoginPhone = async ()=>{
      if(activeKey.value == '1'){
        formRef.value
        .validate()
        .then(() => {
           store
            .dispatch("user/login", {...formState})
            .then(() => {
              console.log('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyQWNjb3VudCI6ImFkbWluIiwiZXhwIjoxNjQ5MDU3MTM4LCJ1c2VySWQiOiI4NUJGQzAwMkJGMzA0MjNFQkQxMUI0RjI5OTE0MUI0NiJ9.2q6d3IXs2HQ0aF5w51snLPlcvGp4dRgWBlQc4H8ge54')
              router.push({ path: '/dashboard' || "/" });
            })
            .catch(() => {
            });
        })
        .catch(error => {
          console.log('error', error);
        });
      }else{
        formRef2.value
        .validate()
        .then(() => {
          store
            .dispatch("user/loginPhone", {...formState2})
            .then(() => {
             router.push({ path: '/dashboard' || "/" });
            })
            .catch(() => {
            });
        })
        .catch(error => {
          console.log('error', error);
        });
      }
    }

    let validatePhone = async (rule, value) => {
      // var reg = 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/;
      var reg = /^1(3|4|5|6|7|8|9)\d{9}$/;
      //var url="/nptOfficialWebsite/apply/sendSms?mobile="+this.ruleForm.phone;
      if (value == "") {
        return Promise.reject("请输入手机号");
      } else if (!reg.test(value)) {
        return Promise.reject("请输入正确手机号");
      } else {
        return Promise.resolve();
      }
    };
    const rules2 = {
      phone: [
        {
          required: true,
          validator: validatePhone,
          message: "请输入正确手机号",
          trigger: "blur",
        },
      ],
      smsCode: [
        {
          required: true,
          message: "请输入验证码",
          trigger: "blur",
        },
      ],
    };

    const formRef2 = ref();
    const showTime = ref(false);
    const timer = ref(60)
    const formState2 = reactive({
      phone: "",
      smsCode: "",
    });
  
    // 获取验证码
    const getCode = async () => {
      console.log(1234)
      console.log(showTime.value)
      if (!showTime.value) {
        if (!formState2.phone) {
          message.error("请输入手机号");
          return;
        } else if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(formState2.phone)) {
          message.error("手机号有误,请重填");
          return;
        }
         timer.value = 60;
      showTime.value = true;
      let settimer = null;
      if (settimer) {
        clearInterval(settimer);
      }
      userApi.sendMessage({ phone: formState2.phone }).then((res) => {
        if (res && res.success) {
          settimer = setInterval(() => {
            if (timer.value <= 0) {
              showTime.value = false;
              clearInterval(settimer);
            } else {
              timer.value--;
            }
          }, 1000);
        }
      });
      }
    };

    return {
      activeKey,
      rules,
      formState,
      formRef,
      handleLoginPhone,
      handleRegister,
      handleForgetFace,
      formState2,
      formRef2,
      rules2,
      showTime,
      timer,
      getCode,

      wrapperCol: {
        span: 24,
      },
    };
  },
});
</script>
<style lang="scss">
$bg: #fff;
$light_gray: #ccc;
$cursor: #333;
.form-rright{
  .ant-tabs-nav-scroll{
    text-align: center;
  }
   input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      color: $light_gray;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  
}
  
</style>
<style lang="scss" scoped>
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
/* reset element-ui css */
.login-container {
  .el-tabs__item {
    color: #fff;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item-1 {
    border: 1px solid #d7dadb;
    background: #fff;
    border-radius: 2px;
    height: 40px;
    line-height: 40px;
    color: #a5a7a8;
  }
  .login-form-content {
    width: 800px;
    height: 500px;
    display: flex;
    margin: 0 auto;
    margin-top: 200px;
    .login-bg {
      width: 360px;
      height: 100%;
      img {
        font-size: 0;
        width: 100%;
        height: 100%;
      }
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #34bbff;
$dark_gray: #333;
$light_gray: #333;
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  background: url("../../assets/bg.png") center center no-repeat;
  background-size: cover;
  overflow: hidden;

  .login-form {
    position: relative;
    max-width: 100%;
    overflow: hidden;
    box-sizing: border-box;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    height: 40px;
    line-height: 40px;
    box-sizing: border-box;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 30px;
      color: $light_gray;
      text-align: center;
      font-weight: bold;
      margin-top: 52px;
    }
    .titleEn {
      font-size: 12px;
      color: $light_gray;
      margin: 10px 0;
      text-align: center;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .show-pwd-code {
    position: absolute;
    right: 10px;
    font-size: 16px;
    color: #34bbff;
    cursor: pointer;
    user-select: none;
  }
}
.form-rright {
  background: #fff;
  margin: 10px 0;
  color: $light_gray !important;
  flex-direction: column;
  padding: 0 70px;
  box-sizing: border-box;
  width: 440px;
  border-top-right-radius: 8px;
  border-bottom-right-radius: 8px;
}
.el-tabs {
  width: 100%;
  ::v-deep .el-tabs__nav {
    width: 100%;
  }
  ::v-deep .el-tabs__item {
    width: 50% !important;
    color: #a5a7a8 !important;
    padding: 0;
    font-size: 16px;
    text-align: center;
  }
  ::v-deep .el-tabs__active-bar {
    width: 50% !important;
    background: #34bbff;
  }
  ::v-deep .is-active {
    color: #34bbff !important;
  }
  ::v-deep .el-form-item__content {
    height: 38px;
    line-height: 38px;
  }
  ::v-deep .el-input {
    height: 38px;
    line-height: 38px;
    input {
      height: 38px;
      line-height: 38px;
      box-sizing: border-box;
      color: #a5a7a8;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: #7c7e82 !important;
      }
    }
  }
  .svg-icon {
    vertical-align: 0.35em;
  }
}
.color-gray {
  color: #a5a7a8 !important;
}
::v-deep .el-tabs__header {
  margin-bottom: 30px;
}
::v-deep .el-input__inner {
  border: none !important;
}
</style>
